import java.util.HashMap;

public class LongestSubstring {

    public int longestSubstring(String s, int k) {
        HashMap<Character, Integer> maps = new HashMap<>();
        char[] chars = s.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            maps.put(chars[i], maps.getOrDefault(chars[i], 0) + 1);
        }
        int ans = 0;
        for (Character c : maps.keySet()) {
            if (maps.get(c) < k) {
                for (String temp : s.split(c + "")) {
                    ans = Math.max(ans, longestSubstring(temp, k));
                }
                return ans;
            }
        }
        return chars.length;
    }
}
